

clear
gen t = .

tempfile dat
save `dat'

* Iterate along calendar number around launch
*forvalues t = -36/60{
forvalues t = -36/60{

	* Month-by-month
	use "${root}\Data\etf_month_data", clear
	keep if t == `t'
	drop q ret
	rename (q4 ret_index) (q ret)

	* EW
	egen ret_e = mean(ret), by(q date)

	* Collapse
	collapse ret_e rf mktrf smb hml rmw cma umd me ia roe, by(q date)

	* Excess returns
	gen exret_e = ret_e - rf

	* % scale
	foreach var of varlist mktrf-exret_e{
		replace `var' = `var' * 100
	}
	
	gen t = `t'
	order q t date 

	append using `dat'
	save `dat', replace

	
}



clear
gen q = .
tempfile alphas_pre
save `alphas_pre'

forvalues q = 1/4{

	use `dat' if q == `q', clear
	keep if t <= 0
	replace t = t + 37

	*forvalues t = 1/97{
	forvalues t = 1/37{
		
		gen mktrf_`t' = mktrf * (t == `t') 
		
	}	
	
	* Seemingly Unrelated Regressions
	reg exret_e bn.t i.t#c.mktrf i.t#c.smb i.t#c.hml i.t#c.umd, hascons cluster(date)


	matrix coef_all = e(b)
	matrix covar_all = e(V)

	matrix coef = coef_all[1,1..97]
	matrix covar = covar_all[1..97,1..97]

	*forvalues t = 1/97{
	forvalues t = 1/37{
	
		* Coefficients and standard errors
		matrix ones_sub = J(1,`t',1)
		matrix coef_sub = coef[1,1..`t']
		matrix covar_sub = covar[1..`t',1..`t']
		matrix alpha_ffc4_sub = ones_sub * coef_sub'
		matrix se_ffc4_sub = ones_sub * covar_sub * ones_sub'
		local alpha_ffc4_sub = alpha_ffc4_sub[1,1]
		local se_ffc4_sub = se_ffc4_sub[1,1]
		
		clear
		set obs 1
		gen q = `q'
		gen t = (`t' - 37)
		gen alpha_ffc4 = `alpha_ffc4_sub'
		gen se_ffc4 = sqrt(`se_ffc4_sub')

		append using `alphas_pre'
		save `alphas_pre', replace
		
	}
}

clear
gen q = .
tempfile alphas_post
save `alphas_post'

forvalues q = 1/4{

	use `dat' if q == `q', clear
	keep if t >= 0
	replace t = t + 37

	*forvalues t = 1/97{
	forvalues t = 37/97{
		
		gen mktrf_`t' = mktrf * (t == `t') 
		
	}	
	
	* Seemingly Unrelated Regressions
	reg exret_e bn.t i.t#c.mktrf i.t#c.smb i.t#c.hml i.t#c.umd, hascons cluster(date)


	matrix coef_all = e(b)
	matrix covar_all = e(V)

	matrix coef = coef_all[1,1..97]
	matrix covar = covar_all[1..97,1..97]

	*forvalues t = 1/97{
	forvalues t = 37/97{
	
		* Coefficients and standard errors
		local t = `t' - 36
		matrix ones_sub = J(1,`t',1)
		matrix coef_sub = coef[1,1..`t']
		matrix covar_sub = covar[1..`t',1..`t']
		matrix alpha_ffc4_sub = ones_sub * coef_sub'
		matrix se_ffc4_sub = ones_sub * covar_sub * ones_sub'
		local alpha_ffc4_sub = alpha_ffc4_sub[1,1]
		local se_ffc4_sub = se_ffc4_sub[1,1]
		
		clear
		set obs 1
		gen q = `q'
		gen t = `t' - 1
		gen alpha_ffc4 = `alpha_ffc4_sub'
		gen se_ffc4 = sqrt(`se_ffc4_sub')

		append using `alphas_post'
		save `alphas_post', replace
		
	}
}


use `alphas_pre', clear

gen temp0 = alpha_ffc4 if t == 0 
egen temp = mean(temp0), by(q)
replace alpha_ffc4 = alpha_ffc4 - temp

gen car = alpha_ffc4

gen upper = car + 1.96*se_ffc4
gen lower = car - 1.96*se_ffc4



* Latex
keep t q car upper lower
reshape wide car upper lower, i(t) j(q)

replace t = -0.05 if t == 0

outsheet using "${root}\Data\fig_indexed_performance_1.csv", comma replace





use `alphas_post', clear

replace alpha_ffc4 = 0 if t == 0
replace se_ffc4 = 0 if t == 0

gen car = alpha_ffc4

gen upper = car + 1.96*se_ffc4
gen lower = car - 1.96*se_ffc4

* Latex
keep t q car upper lower
reshape wide car upper lower, i(t) j(q)
replace t = 0.05 if t == 0
outsheet using "${root}\Data\fig_indexed_performance_2.csv", comma replace












